package pokeronlab_2014.mcts;

import pokeronlab_2014.Controller;
import pokeronlab_2014.mcts.TreeNode.NodeType;

public abstract class PlayerNode extends TreeNode {
	
	public void expand(TreeNode currentNode) {
		int numberOfC=nActions;
		if(rules.maxbet==0)
			numberOfC--;
		
		children = new TreeNode[numberOfC];
		
		
		
		//Controller.debugWindow.printMessage("PlayerNode.expand");
		for (int i = 0; i < numberOfC; i++) {
			if(rules.nextplayer(rules.actualplayer, i)==rules.myseat)
				children[i] = new BotNode();
			else
				if(rules.nextplayer(rules.actualplayer, i)==-1)
					children[i] = new CardNode();
				else
					children[i] = new OpponentNode();
			
			

				
			
			if(i==0)
			{
				
//				String tmp = "rules.actualplayer=" + rules.actualplayer;
//				Controller.debugWindow.printMessage(tmp);					
				children[i].creatNode(this,i, rules.getPlayerID(rules.nextplayer(rules.actualplayer, i)), 0, level);
			}
			if(i==1)
				children[i].creatNode(this,i, rules.getPlayerID(rules.nextplayer(rules.actualplayer, i)), rules.amunttocall(rules.actualplayer), level);
			if(i==2)
				if(rules.round<2)
					children[i].creatNode(this,i, rules.getPlayerID(rules.nextplayer(rules.actualplayer, i)), rules.amunttocall(rules.actualplayer)+rules.fixlimit, level);
				else
					children[i].creatNode(this,i, rules.getPlayerID(rules.nextplayer(rules.actualplayer, i)), rules.amunttocall(rules.actualplayer)+2*rules.fixlimit, level);
			//}
			//betet be kell állítani
		}
	}
	
	public TreeNode select(TreeNode currentNode) {
		
		//Controller.debugWindow.printMessage("PlayerNode.select");
		
		//currentNode.typeOfNode= NodeType.playeraction_my;
		
		if (currentNode.isLeaf())
			currentNode.expand(currentNode);
		
		double children0tmp=1;
		double children1tmp=1;
		double children2tmp=1;
		
		
		
		if(rules.maxbet!=0)
		{
		if (currentNode.children[0].nVisits == 0)
			currentNode = currentNode.children[0];
		else if (currentNode.children[1].nVisits == 0)
			currentNode = currentNode.children[1];
		else if (currentNode.children[2].nVisits == 0)
			currentNode = currentNode.children[2];
		else {
			children0tmp = currentNode.children[0].totValue
					+ c
					* Math.sqrt(Math.log10(nVisits)
							/ currentNode.children[0].nVisits);
			children1tmp = currentNode.children[1].totValue
					+ c
					* Math.sqrt(Math.log10(nVisits)
							/ currentNode.children[1].nVisits);
			children2tmp = currentNode.children[2].totValue
					+ c
					* Math.sqrt(Math.log10(nVisits)
							/ currentNode.children[2].nVisits);
			
			double children0 = children0tmp
					/ (children0tmp + children1tmp + children2tmp);
			double children1 = children1tmp
					/ (children0tmp + children1tmp + children2tmp);
//			double children2 = children2tmp
//					/ (children0tmp + children1tmp + children2tmp);
			
			double random = Math.random();
			if (random < children0)
				currentNode = currentNode.children[0];
			else {
				if (random < children0 + children1)
					currentNode = currentNode.children[1];
				else {
					//if (random < children0 + children1 + children2)
					if(rules.maxbet==0)
						currentNode = currentNode.children[1];
					else
						currentNode = currentNode.children[2];
				}
			}
		}
		}
		else
		{
			if (currentNode.children[0].nVisits == 0)
				currentNode = currentNode.children[0];
			else if (currentNode.children[1].nVisits == 0)
				currentNode = currentNode.children[1];
			else {
				children0tmp = currentNode.children[0].totValue
						+ c
						* Math.sqrt(Math.log10(nVisits)
								/ currentNode.children[0].nVisits);
				children1tmp = currentNode.children[1].totValue
						+ c
						* Math.sqrt(Math.log10(nVisits)
								/ currentNode.children[1].nVisits);
				
				
				double children0 = children0tmp
						/ (children0tmp + children1tmp );
//				double children2 = children2tmp
//						/ (children0tmp + children1tmp + children2tmp);
				
				double random = Math.random();
				if (random < children0)
					currentNode = currentNode.children[0];
				else 
					currentNode = currentNode.children[1];

			}
			
		}
		String tmp3 ="children0tmp=" + children0tmp + ", children1tmp=" + children1tmp +", children2tmp=" + children2tmp + ", level=" + level;
		//Controller.debugWindow.printMessage(tmp3);
		
		
		return currentNode;
	}
	
	public abstract TreeNode simulate(TreeNode currentNode);
}
